package com.turbo.boot.crawler.spider.worker.impl.quota;

import com.alibaba.fastjson.JSON;
import com.turbo.boot.crawler.dao.dal.CrawlerResultDal;
import com.turbo.boot.crawler.dao.entity.CrawlerResultEntity;
import com.turbo.boot.crawler.spider.worker.BaseTurboApiCrawlerPageParser;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 描述
 *
 * @author wuyn
 * @since 2024/4/19
 */
@Service("cnpiCrawlerPageParser")
public class CnpiCrawlerPageParser extends BaseTurboApiCrawlerPageParser<CnpiCrawlerPageParser.PageVo> {


    @Resource
    private CrawlerResultDal crawlerResultDal;

    @Override
    public void parse(PageVo pageVo) {
        // 解析封装 PageVo 对象
        if (StringUtils.isEmpty(pageVo.getT_periodical())) {
            return;
        }
        System.out.println(pageVo.toString());

        List<CrawlerResultEntity> batch = new ArrayList<>();

        DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd");
        DateTime dateTime = formatter.parseDateTime(pageVo.getT_posteddate());
        // 如果需要转换为java.util.Date
        Date utilDate = dateTime.toDate();


        // 处理业务代码
        CrawlerResultEntity cnpi = buildCrawlerResultEntity();
        cnpi.setCollectedAt(utilDate);
        cnpi.setQuotaCode("CNPI");
        cnpi.setQuotaName("CNPI");
        cnpi.setQuotaValue(pageVo.getT_cnpi());

        batch.add(cnpi);


        CrawlerResultEntity cndpi = buildCrawlerResultEntity();
        cndpi.setCollectedAt(utilDate);
        cndpi.setQuotaCode("CNDPI");
        cndpi.setQuotaName("CNDPI");
        cndpi.setQuotaValue(pageVo.getT_cndpi());

        batch.add(cndpi);


        CrawlerResultEntity cntpi = buildCrawlerResultEntity();
        cntpi.setCollectedAt(utilDate);
        cntpi.setQuotaCode("CNTPI");
        cntpi.setQuotaName("CNTPI");
        cntpi.setQuotaValue(pageVo.getT_cntpi());

        batch.add(cntpi);


        CrawlerResultEntity cncpi = buildCrawlerResultEntity();
        cncpi.setCollectedAt(utilDate);
        cncpi.setQuotaCode("CNCPI");
        cncpi.setQuotaName("CNCPI");
        cncpi.setQuotaValue(pageVo.getT_cncpi());

        batch.add(cncpi);

        if (CollectionUtils.isNotEmpty(batch)) {
            crawlerResultDal.insertBatch(batch);
        }

    }


    @Setter
    @Getter
    public static class PageVo {
        String t_cncpi;
        String t_cndpi;
        String t_cnpi;
        String t_cntpi;
        String t_dwt;
        String t_id;
        String t_periodical;
        String t_posteddate;
        String t_trading;

        @Override
        public String toString() {
            return JSON.toJSONString(this);
        }
    }
}
